/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jfNMS.java
 *
 * Created on 01/10/2011, 21:13:34
 */
package visao;

import agent.AgentMBean;
import util.Computer;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import mbeanserver.MBServer;
import util.ThreadMBeanServer;

/**
 *
 * @author alan
 */
public class jfNMS extends javax.swing.JFrame {

    MBeanServerConnection JMXSC;
    DefaultListModel modeloLista = new DefaultListModel();
    Computer pc;
    ObjectName linux;
    ObjectName windows;
    ObjectName controle;
    MBServer mbserver;
    Thread escutaMBeanServer;
    jfDiscoWindows jfDiscoWindows;
    jfMemoria jfMemoriaWindows;

    public jfNMS() {
        initComponents();
        setLocationRelativeTo(this);
        try {
            jbParar.setVisible(false);
            mbserver = new MBServer();
            linux = new ObjectName("tcc:type=MACHINELINUX");
            windows = new ObjectName("tcc:type=MACHINEWINDOWS");
        } catch (RemoteException ex) {
            Logger.getLogger(jfNMS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(jfNMS.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedObjectNameException ex) {
            Logger.getLogger(jfNMS.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jPopupMenu2 = new javax.swing.JPopupMenu();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlPCInstrumentados = new javax.swing.JList();
        jbParar = new javax.swing.JButton();
        jbIniciar = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jmiDiscoWindows = new javax.swing.JMenuItem();
        jmtMemoriaLivre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("NMS");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Computadores Instrumentados"));

        jlPCInstrumentados.setModel(modeloLista);
        jlPCInstrumentados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlPCInstrumentadosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jlPCInstrumentados);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
        );

        jbParar.setText("Parar");
        jbParar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbPararActionPerformed(evt);
            }
        });

        jbIniciar.setText("Iniciar busca de computadores ");
        jbIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbIniciarActionPerformed(evt);
            }
        });

        jMenu1.setText("Opções");

        jmiDiscoWindows.setText("Listar os computadores com baixa capacidade de espaço livre em disco");
        jmiDiscoWindows.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiDiscoWindowsActionPerformed(evt);
            }
        });
        jMenu1.add(jmiDiscoWindows);

        jmtMemoriaLivre.setText("Listar os computadores com baixa quantidade de memória livre");
        jmtMemoriaLivre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmtMemoriaLivreActionPerformed(evt);
            }
        });
        jMenu1.add(jmtMemoriaLivre);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbParar, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbIniciar))
                .addContainerGap(208, Short.MAX_VALUE))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbIniciar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbParar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jlPCInstrumentadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jlPCInstrumentadosMouseClicked


    pc = (Computer) modeloLista.get(jlPCInstrumentados.getSelectedIndex());
    JMXSC = AgentMBean.makeAgent(pc.getIp());

    try {
        if (pc.getOs().equals("MACHINELINUX")) {
            jfLinux jfLinux = new jfLinux(JMXSC, this);
            jfLinux.setVisible(true);
        } else {
            if (pc.getOs().equals("MACHINEWINDOWS")) {
                jfWindows jfWindows = new jfWindows(JMXSC, this);
                jfWindows.setVisible(true);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Erro ao tentar abrir a janela de gerência !");
    }



}//GEN-LAST:event_jlPCInstrumentadosMouseClicked

private void jbIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbIniciarActionPerformed
    escutaMBeanServer = new Thread(new ThreadMBeanServer(mbserver.getMBServer(), modeloLista, jlPCInstrumentados));
    escutaMBeanServer.setPriority(1);
    escutaMBeanServer.start();

    jbIniciar.setVisible(false);
    jbParar.setVisible(true);
}//GEN-LAST:event_jbIniciarActionPerformed

private void jbPararActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbPararActionPerformed
    modeloLista.removeAllElements();
    escutaMBeanServer.stop();
    jbParar.setVisible(false);
    jbIniciar.setVisible(true);
}//GEN-LAST:event_jbPararActionPerformed

private void jmiDiscoWindowsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiDiscoWindowsActionPerformed
    if (jfDiscoWindows == null) {
        jfDiscoWindows = new jfDiscoWindows(mbserver);
    }
    jfDiscoWindows.setVisible(true);
}//GEN-LAST:event_jmiDiscoWindowsActionPerformed

private void jmtMemoriaLivreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmtMemoriaLivreActionPerformed
    if (jfMemoriaWindows == null) {
        jfMemoriaWindows = new jfMemoria(mbserver);
    }
    jfMemoriaWindows.setVisible(true);
}//GEN-LAST:event_jmtMemoriaLivreActionPerformed

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel".equals(info.getClassName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(jfDiscoWindows.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(jfDiscoWindows.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(jfDiscoWindows.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(jfDiscoWindows.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new jfNMS().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPopupMenu jPopupMenu2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbIniciar;
    private javax.swing.JButton jbParar;
    private javax.swing.JList jlPCInstrumentados;
    private javax.swing.JMenuItem jmiDiscoWindows;
    private javax.swing.JMenuItem jmtMemoriaLivre;
    // End of variables declaration//GEN-END:variables
}
